<?php
/**
 * @file
 * @author jarrod.swift
 */
namespace ORM;
/**
 * The ModelCollection class is used as a container for multiple ORM_Interface classes
 * 
 * This allows groups of objects to be used easily, such as \c $software->tickets->save();
 * 
 * A modelcollection is returned when using ORM_Model::FindAll():
 * @code
 * $model_collection_of_people = People::FindAll();
 * @endcode
 * 
 * @see ORM_Interface, ORM_Model, ObjectCollection
 */
class ModelCollection extends ObjectCollection {
    /**
     * Save each object in the collection
     *
     * Returns an array of ids matching the objects.
     *
     * <b>Usage</b>
     * @code
     * $people = Person::FindAll();
     *
     * $people[1]->name = 'changed';
     * $people[]        = new Person( array('name' => 'new person' ) );
     * $ids             = $people->save();
     * @endcode
     *
     * @return array
     *      Of row ids. This may be especially useful when using autoincrement
     *      style keys that are generated by the database
     */
    public function save() {
        return $this->map(function($item) {
            $item->save();
            return $item->id();
        });
    }

    /**
     * Delete each object in the collection
     *
     * Convenient shortcut to writing an anonymous function to ObjectCollection::each()
     *
     * <b>Usage</b>
     * @code
     * // This will delete every people object in the database
     * $people = Person::FindAll();
     * $people->delete();
     * @endcode
     *
     */
    public function delete() {
        $this->each(function($item){
            $item->delete();
        });
    }

    
}
